package com.zhou.common.quartz.test.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.autoconfigure.quartz.QuartzTransactionManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author xianfeng
 * @date 2023/12/22 11:19
 * @description:
 */
@Configuration
public class QuartzDataSourceConfiguration {

    /**
     * 要让quartz 单独使用一个数据库时需要加上@QuartzDataSource
     * 否则会使用主数据源
     * 创建 quartz 数据源的配置对象
     */
    @Bean
    @QuartzDataSource
    @ConfigurationProperties(prefix = "spring.datasource.quartz")
    public DataSource quartzDataSource() {
        return DruidDataSourceBuilder.create().build();
    }


    /**
     * 要让quartz 单独使用一个transactionManager时需要加上@QuartzTransactionManager
     * 创建 quartz 数据源的配置对象
     */
    @Bean
    @QuartzTransactionManager
    public DataSourceTransactionManager paperPushSystemTransactionManager() {
        return new DataSourceTransactionManager(quartzDataSource());
    }

}
